# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: utils.py
@time: 2021/12/4 19:27
'''
'''
pip install itsdangerous
    用于邮箱验证中 加密user_id
'''

from tuling_mall.settings import SECRET_KEY
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

#加密
def generic_email_verify_token(user_id):
    # 创建实例
    s = Serializer(secret_key = SECRET_KEY,expires_in = 3600 * 24)
    # 加密数据
    data = s.dumps({'user_id':user_id})
    # 返回数据 需进行编码
    return data.decode()

# 解密
def check_verify_token(token):
    s = Serializer(secret_key = SECRET_KEY,expires_in = 3600 * 24)
    try:
        result = s.loads(token)
    except Exception as e:
        print(e)
        return None
    return result.get('user_id')

